#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "A2C.h"
#include "example-1.h"

int main(int argc, char * argv[])
{
    Str1 *              stra_ptr; /* Will allocate with A2C */
    Str1                strd; /* Allocated on our own */
    Str1 *              strDecode_ptr = 0;
    A2C_STREAM *        encodeStream_ptr = 0;
    A2C_STREAM *        printStream_ptr = 0;
    A2C_ERROR           err;
    int                 i;
    char *              string_buff_ptr;
    unsigned char *     data_buff_ptr;
    int                 buff_len;
    
    /*  Allocate the data structure */
    err = A2C_Alloc_Str1(&stra_ptr);
    if (err < A2C_ERROR_Success) { exit(1); }  /* Allocation error */
    /*  Set the value of stra_ptr */
    stra_ptr->string = strdup("abc");
    /*  Set up the strd structure */
    A2C_Init_Str1(&strd);
    strd.string = "def";
    /* Print out the two strings */
    err = A2C_CreateMemoryStream(&printStream_ptr);
    if (err < A2C_ERROR_Success) { exit(1); }  /* Allocation error */
    err = A2C_Print_Str1(stra_ptr, printStream_ptr);
    if (err < A2C_ERROR_Success) { exit(1); }  /* Filling error */
    err = A2C_GetStringFromStream(printStream_ptr, &string_buff_ptr);
    printf("The value of stra_ptr is %s\n", string_buff_ptr);
    err = A2C_Print_Str1(&strd, printStream_ptr);
    if (err < A2C_ERROR_Success) { exit(1); }  /* Filling error */
    err = A2C_GetStringFromStream(printStream_ptr, &string_buff_ptr);
    printf("The value of strd is %s\n", string_buff_ptr);

    /*  Compare the strings */
    printf("First compare: ");
    if (A2C_Compare_Str1(stra_ptr, &strd) == 0) {
        printf("The strings are the same\n");
    } else {
        printf("The strings are different\n");
    }
    /* Make them the same by copying stra into strd */
    err = A2C_Copy_Str1(&strd, stra_ptr);
    if (err < A2C_ERROR_Success) { exit(1); }  /* Copying error */
    printf("Second compare: ");
    if (A2C_Compare_Str1(stra_ptr, &strd) == 0) {
        printf("The strings are the same\n");
    } else {
        printf("The strings are different\n");
    }

    /*  Encode stra */
    err = A2C_CreateMemoryStream(&encodeStream_ptr);
    if (err < A2C_ERROR_Success) { exit(1); }  /* Allocation error */
    err = A2C_EncodeDer_Str1(stra_ptr, 0, NULL, encodeStream_ptr);
    if (err < A2C_ERROR_Success) { exit(1); }  /* Encoding error */
    /*  Get the data from the stream and print it as hex */
    err = A2C_GetDataFromStream(encodeStream_ptr, &data_buff_ptr, &buff_len);
    if (err < A2C_ERROR_Success) { exit(1); }  /* Getting error */
    printf("The bytes (in hex) were: ");
    for (i=0; i<buff_len; i++) printf("%02x ", data_buff_ptr[i]);
    printf("\n");

    /*  Now do the decode on what we just encoded */
    err = A2C_DecodeDer_Str1(&strDecode_ptr, 0, NULL, data_buff_ptr, buff_len);
    if (err < A2C_ERROR_Success) { exit(1); }  /* Decoding error */
    /*  See if this is the same as the original; it should be */
    printf("Third compare: ");
    if (A2C_Compare_Str1(strDecode_ptr, stra_ptr) == 0) {
        printf("The decode is the same as the original\n");
    } else {
        printf("The decode is different the original\n");
    }

    /*  Free up what we allocated */
    A2C_Free_Str1(stra_ptr);
    A2C_Free_Str1(strDecode_ptr);
    A2C_FreeStream(encodeStream_ptr);
    A2C_FreeStream(printStream_ptr);
    free(data_buff_ptr);
    free(string_buff_ptr);
    exit(0);
}
